import Taro from "@tarojs/taro";
import type {} from "@tarojs/taro";

const TIME_OUT = 60000;
const BASE_URL = "http://codercba.com:9060/juanpi/api";

interface RequestParams {
  url: string;
  method:
    | "OPTIONS"
    | "GET"
    | "HEAD"
    | "POST"
    | "PUT"
    | "PATCH"
    | "DELETE"
    | "TRACE"
    | "CONNECT";
  data: any;
  header?: any;
  timeout?: number;
  loadingTitle?: string;
  [key: string]: any;
}

class AskService {
  private baseURL: string;
  private timeout: number;
  constructor(baseURL: string, timeout: number) {
    this.baseURL = baseURL;
    this.timeout = timeout;
  }

  request(options: RequestParams): Promise<any> {
    return new Promise((resolve, reject) => {
      Taro.request({
        url: this.baseURL + options.url,
        method: options.method || "GET",
        timeout: this.timeout,
        data: options.data,
        success(res) {
          resolve(res.data);
        },
        fail(err) {
          reject(err);
        },
      });
    });
  }

  get(url: string, params: any) {
    return this.request({ url, method: "GET", data: params });
  }

  post(url: string, data: any) {
    return this.request({ url, method: "POST", data });
  }
}
export default new AskService(BASE_URL, TIME_OUT);
